<?php

/**
 * @author Edison P. Quinones Jr
 * @version  1.0.0
 * @copyright 2008
 */

class CreatejumpsiteController extends Zend_Controller_Action
{
	function init()
	{
		$this->initView();
		$this->view->baseUrl = $this->_request->getBaseUrl();
		$webInfoNamespace  = new Zend_Session_Namespace('webInfo');
		$this->view->webInfo = $webInfoNamespace->webInfo;
		Zend_Loader::loadClass('Zend_Filter_StripTags');
		Zend_Loader::loadClass('Category');
		Zend_Loader::loadClass('GroupAvatar');
		Zend_Loader::loadClass('Favicon');
		Zend_Loader::loadClass('GroupProfile');
		Zend_Loader::loadClass('GroupjumperAll');
		Zend_Loader::loadClass('Page');
		$countWords = strlen($_SERVER['REQUEST_URI']);
		if (strpos($_SERVER['REQUEST_URI'], '/', $countWords-1) == true)
		{
			$this->_redirect(substr_replace($_SERVER['REQUEST_URI'], '', $countWords-1));
			//$this->view->testingLang = substr_replace($_SERVER['REQUEST_URI'], '', $countWords-1);
		}

		$this->view->user = Zend_Auth::getInstance()->getIdentity();

		# Links
		$registry = Zend_Registry::getInstance();
		$this->view->links = $registry->get('linksArray');
		# end of links
	}
	function indexAction()
	{
		$filter = new Zend_Filter_StripTags();
		$webInfoNamespace  = new Zend_Session_Namespace('webInfo');
		$webInfo = $webInfoNamespace->webInfo;
		$auth = Zend_Auth::getInstance();
		if (!$auth->hasIdentity())
		{
			$this->_redirect($webInfo."createjumpsite/notlogin");
		}

		if ($this->_request->isPost())
		{
			$error = false;
			$groupName = trim($filter->filter($this->_request->getPost('groupName')));
			$groupDescription = trim($filter->filter($this->_request->getPost('groupDescription')));
			$accessType = trim($filter->filter($this->_request->getPost('accessType')));
			$jumpsiteCategory = trim($filter->filter($this->_request->getPost('jumpsiteCategory')));
			$groupAvatarId = trim($filter->filter($this->_request->getPost('groupAvatar')));
			$faviconId = trim($filter->filter($this->_request->getPost('favicon')));
			
			if (($groupName == null) || ($groupDescription == null))
			{
				$error = true;
				$this->view->jumpsiteNameDescriptionError = 'Please input the Group name and Description';
			}
			

			if ($error == false)
			{
				$groupProfile = new GroupProfile();
				$groupId = array (
				'name' => $groupName,
				'description' => $groupDescription,
				'access' => $accessType,
				'category_id' => $jumpsiteCategory,
				'avatar_id' => $groupAvatarId,
				'favicon_id' => $faviconId,
				'owner_id' => Zend_Auth::getInstance()->getIdentity()->id);
				$groupId = $groupProfile->insert($groupId);

				$groupjumperAll = new GroupjumperAll();
				$groupjumperAll->insert(
				array(
				'jumper_id' => Zend_Auth::getInstance()->getIdentity()->id,
				'group_id' => $groupId
				)
				);

				$page = new Page();
				$page->insert(
				array(
				'name' => 'Home',
				'url' => '',
				'desc' => '',
				'top_wallpaper' => 1,
				'body_wallpaper' => 1,
				'layout_id' => 1,
				'group_id' => $groupId,
				'theme_id' => 1
				)
				);
				
				$this->_redirect($webInfo.'Createjumpsite/Add');
			}

		}


		# check if user flag_confirm is equal to zero
		$this->view->userInstance = Zend_Auth::getInstance()->getIdentity();
		# end of checking user flag_confirm is equal to zero

		$this->view->headerAdditional = '
		<link rel="stylesheet" type="text/css" href="'.$webInfo.'css/registration.css" />
    	<link rel="stylesheet" type="text/css" href="'.$webInfo.'css/testing_for.css" />   	
		';

		$category = new Category();
		$this->view->jumpsiteCategory = $category->fetchAll(null, 'id');
		//fetchAll($where, $order, $limit, $offset);


		$groupAvatar = new GroupAvatar();
		$this->view->groupAvatar = $groupAvatar->fetchAll(null, 'id');

		$favicon = new Favicon();
		$this->view->faviconList = $favicon->fetchAll(null, 'id');


		$this->view->title = 'Create a jumpsite';
	}
	function notloginAction()
	{
		$webInfoNamespace  = new Zend_Session_Namespace('webInfo');
		$webInfo = $webInfoNamespace->webInfo;
		$this->view->headerAdditional = '<script type="text/javascript" src="'.$webInfo.'jscripts/msg-box2.js"></script>';

		$confirmationData = array ('last_visited' => $lastVisited, 'verification_url' => $verification_url);
		$confirmationId = $confirmation_list->insert($confirmationData);
	}
	function addAction()
	{
		$filter = new Zend_Filter_StripTags();
		$webInfoNamespace  = new Zend_Session_Namespace('webInfo');
		$webInfo = $webInfoNamespace->webInfo;
		$this->view->headerAdditional = '
		<link rel="stylesheet" type="text/css" href="'.$webInfo.'css/registration.css" />
		';
	}
}